home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / SCAN.AML < prev    next >
Text File  |  1996-07-17  |  6KB  |  237 lines

  1. //--------------------------------------------------------------------
  2. // SCAN.AML
  3. // Scan Files, (C) 1993-1996 by nuText Systems
  4. //
  5. // (see Scan.dox for user help)
  6. //
  7. // This macro searches multiple files on disk for a search string or
  8. // regular expression pattern.
  9. //
  10. // The following search options can be specified:
  11. //
  12. //   i - ignore case
  13. //   u - search within subdirectories also
  14. //   w - whole words only
  15. //   x - regular expressions
  16. //
  17. // If a directory buffer is passed to the macro (arg 3), then only then
  18. // only the files in the directory buffer are searched, otherwise the
  19. // user us prompted to enter a directory or file specification where
  20. // files will be searched.
  21. //
  22. // After the search has completed, a file manager window will be
  23. // displayed listing all the files where the search string was found.
  24. // Selecting a file from the file manager will move the cursor to the
  25. // first oeccurrence of the search string. The 'findlast' command can be
  26. // used to find other occurrences.
  27. //
  28. // This macro calls the Scandlg macro.
  29. //
  30. // Usage:
  31. //
  32. // Select this macro from the Macro List (on the Macro menu), or run it
  33. // from the macro picklist <shift f12>.
  34. //
  35. // This macro can be run by selecting the Scan Files item on the edit
  36. // window Search menu or the file manager window Command menu. If run
  37. // from the file manager window Command menu, then only the files listed
  38. // in the file manager will be searched.
  39. //--------------------------------------------------------------------
  40.  
  41. // compile time macros and function definitions
  42. include  bootpath "define.aml"
  43.  
  44. // scan status window dimensions
  45. constant scan_qwidth  = 70  // (for fully qualified scans only)
  46. constant scan_height  = 16
  47.  
  48. // scan status window colors
  49. constant scan_border_color        = color white on gray
  50. constant scan_text_color          = color black on gray
  51. constant scan_found_color         = color brightgreen on gray
  52. constant scan_south_title_color   = color darkgray on green
  53.  
  54. // this object inherits functions from 'fmgr'
  55. settype "fmgr"
  56.  
  57. // use a file list, if passed to this macro
  58. if bufferflag "d?" (arg 3) then
  59.   passedbuf = arg 3
  60. end
  61.  
  62. // create status window
  63. private function createstatus (width)
  64.   createwindow
  65.   setframe ">b"
  66.   setcolor  border_color   scan_border_color
  67.   setcolor  text_color     scan_text_color
  68.   settitle "Scanning" 'c'
  69.   setborder "1i"
  70.   setshadow 2 1
  71.  
  72.   // center the window
  73.   height = scan_height
  74.   ox = (getvidcols - width) / 2
  75.   oy = (getvidrows - height) / 2
  76.   sizewindow ox oy ox + width oy + height "ad"
  77. end
  78.  
  79.  
  80. variable searchstr, filespec, options, delimit
  81. variable fileoptions, diroptions, subdir
  82.  
  83. // scan the current buffer
  84. private function scanfiles
  85.   loop
  86.     // get the next file
  87.     file = fgetfile
  88.  
  89.     // check for subdirectories
  90.     if file [LAST_CHAR] == '\\' then
  91.       delline
  92.       if subdir and not (pos ".." file) then
  93.         insertbuf file + (getname filespec) '' '' fileoptions '' '' (getlines)
  94.         while not (getlinelen (getlines)) do
  95.           delline 1 (getlines)
  96.         end
  97.         insertbuf file + "*.*" '' '' diroptions  '' '' (getlines)
  98.       elseif not getlinelen then
  99.         break
  100.       end
  101.  
  102.     else
  103.       writestr file
  104.       display
  105.       if scanfile file searchstr options delimit then
  106.         writestr "FOUND" scan_found_color (getcoord 'x1') - 7
  107.         if not down then
  108.           break
  109.         end
  110.       else
  111.         if delline > getrow or (not getlinelen) then
  112.           break
  113.         end
  114.       end
  115.       writeline
  116.       display
  117.     end
  118.   end
  119. end
  120.  
  121. macrofile = arg 1
  122.  
  123. // called by Lib.x when a key is entered in the dialog box
  124. function ondialog (keycode)
  125.   // macro help
  126.   if keycode == <f1> then
  127.     helpmacro macrofile
  128.   end
  129. end
  130.  
  131. // get the scan multistring from the user
  132. scanstring = runmacro (bootpath "macro\\scandlg.x") '' passedbuf
  133.  
  134. // use defaults if needed
  135. n = splitstr '' scanstring ref searchstr ref filespec ref options
  136. if n < 3 then
  137.   options = _SearchOpt
  138.   if n < 2 then
  139.     filespec = '.'
  140.   end
  141. end
  142.  
  143. if not scanstring or not searchstr then
  144.   return
  145. end
  146.  
  147. addhistory "_scan" scanstring
  148. addhistory "_find" (joinstr '' searchstr options)
  149.  
  150. // copy passed buffer
  151. if passedbuf then
  152.   markline 1 (getlines) 'T' passedbuf
  153.   scanbuf = if? (getbinarylen passedbuf) (createbbuf) (createbuf)
  154.   copyblock 'T'
  155.   destroymark 'T'
  156.   delline
  157.   filespec = onname (getbufname passedbuf)
  158.   setbufname filespec
  159.  
  160. // load new buffer if none passed
  161. else
  162.   subdir = pos 'u' options
  163.   filespec = onname (qualify filespec (getbufname))
  164.   fileoptions = (sub 'd' '' _FmgrOpt) + 'f' + _NameStyle + (if? subdir 'vq' '')
  165.   scanbuf = loadbuf filespec '' '' fileoptions
  166.   if subdir then
  167.     diroptions = (sub 'f' '' fileoptions) + 'd'
  168.     insertbuf (qualify "*.*" filespec) '' '' diroptions '' '' (getlines)
  169.   end
  170. end
  171.  
  172. if not scanbuf then
  173.   return
  174. end
  175.  
  176. if not getlinelen or not (dir? filespec) then
  177.   destroybuf
  178.   display
  179.   queue "msgbox" filespec + " not found"
  180.   return
  181. end
  182.  
  183. // create scan status window
  184. createstatus (if? (getbinarylen)
  185.                 length (getpath filespec) + 24 scan_qwidth)
  186. display
  187.  
  188. // set line delimiter to use for the scan
  189. delimit = hex2bin _LineDlm
  190.  
  191. scanfiles
  192. breakoff
  193.  
  194. // <ctrl break> was pressed, delete remainder
  195. if down then
  196.   markline getrow - 1 (getlines) 'T'
  197.   deleteblock 'T'
  198. end
  199. destroywindow
  200.  
  201. if getlinelen then
  202.   openbuf scanbuf
  203.   setwinobj (getcurrobj)
  204.  
  205. // not found
  206. else
  207.   destroybuf
  208.   queue "msgbox"  "'" + searchstr + "' not found"
  209.   return
  210. end
  211.  
  212. // stay resident
  213. resident ON
  214.  
  215.  
  216. // scan windows are fmgr windows with the following modifications:
  217.  
  218. key <enter>
  219.   // turn off FmgrQuit temporarily to prevent window from being closed
  220.   fquit = prf.FmgrQuit
  221.   fmgr.FmgrQuit = ''
  222.   pass
  223.   fmgr.FmgrQuit = fquit
  224.   scanstr = joinstr '' searchstr options
  225.   addhistory "_find" scanstr
  226.   // position cursor to first occurrence of the search string
  227.   gotopos 1 (if? (pos 'r' options) (getlines) 1)
  228.   queue "onfound" (search scanstr)
  229. end
  230.  
  231.  
  232. // destroy this object when the scan window is closed
  233. function onclose
  234.   pass
  235.   destroyobject
  236. end
  237.